﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HuntWump2
{
    public partial class Trivia : Form
    {
        TriviaStubsFinal ts;
        Random r = new Random();
        String[] shuffled;
        public int questionNum;
        public int correctAns;
        public int incorrectAns;
        public static bool answerSetCorrect = false;
        
        //constructor here
        public Trivia()
        {
            InitializeComponent();
            ts = new TriviaStubsFinal();
            int rand = r.Next(ts.nOfQuestions);
            questionLabel.Text = ts.questions[rand];
            showAnswer(rand);
            questionNum = rand;

            //answerChoices.Items[0] = "a";
            //answerChoices.Items[1] = "b";
            //answerChoices.Items[2] = "c";
            //answerChoices.Items[3] = "d";

            //.Text = ts.answers[0].getAnswerAt(0);
        }

        /// <summary>
        /// showAnswer shows the answers for each question questionNum
        /// </summary>
        /// <param name="thisQuestionNum">questionNum</param>
        private void showAnswer(int thisQuestionNum)
        {
            shuffled = after(ts.answers[thisQuestionNum].getAnswers());
            //each four answer selection buttons on form below here
            radioButton0.Text = shuffled[0];
            radioButton1.Text = shuffled[1];
            radioButton2.Text = shuffled[2];
            radioButton3.Text = shuffled[3];
        }

        //swap code below
        public String[] swap(String[] lala, int firstIndex, int secondIndex)
        {
            String temp = lala[secondIndex];
            lala[secondIndex] = lala[firstIndex];
            lala[firstIndex] = temp;
            return lala;
        }

        /// <summary>
        /// Either the first or second indexes of the array are swapped with any of the other four answers
        /// </summary>
        /// <param name="lala"></param>
        /// <returns>lala</returns>
        public String[] after(String[] lala1)
        {
            String[] lala = { lala1[0], lala1[1], lala1[2], lala1[3] };
            int rand1 = r.Next(2);
            int rand2 = r.Next(4);
            lala = swap(lala, rand1, rand2);
            return lala;
        }

        int whichButton;
        #region Radio Buttons
        private void radioButton0_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton0.Checked == true)
            {
                whichButton = 0;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                whichButton = 1;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                whichButton = 2;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true)
            {
                whichButton = 3;
            }
        }
        #endregion

        private void ok_Click(object sender, EventArgs e)
        {
            int rand = r.Next(ts.nOfQuestions);
            
            //if the user chooses the correct answer
            if (shuffled[whichButton] == ts.answers[questionNum].getAnswerAt(0))
            {
                correctIncorrect.Text = "Correct! ";
                correctAns++;
            }
            
            //incorrect answer
            else
            {
                correctIncorrect.Text = "I'm sorry, that answer is not correct.";
                incorrectAns++;
            }

            //if user gets 2 questions correct
            if (correctAns == 2)
            {
                //user wins
                correctIncorrect.Text = "Congrats, you correctly answered this set of questions!";
                answerSetCorrect = true;
                Form1.exitwindow(this);
                GameControl.continueTurn();
                //exit the form after this condition has been reached

            }
            //if user gets 2 questions wrong
            else if (incorrectAns == 2)
            {
                //user loses
                correctIncorrect.Text = "I'm sorry, you incorrectly answered this set of questions.";
                Form1.exitwindow(this);
                GameControl.continueTurn();
                //exit the form after this condition has been reached
            }
            else
            {
                questionLabel.Text = ts.questions[rand];
                showAnswer(rand);
                questionNum = rand;
            }
        }

    }
}
